/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hiof.moraa.server.controller;

import com.hiof.moraa.server.service.FeedService;
import com.hiof.moraa.server.service.LocationService;
import com.hiof.moraa.server.service.MapService;
import com.hiof.moraa.server.service.RouteService;
import com.hiof.moraa.server.service.model.Feed;
import com.hiof.moraa.server.service.model.LiveLocationFeed;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author manish
 */
@Controller
public class RaceMapController {

    @Autowired
    LocationService locationService;
    @Autowired
    FeedService feedService;
    @Autowired
    RouteService routeService;
    @Autowired
    MapService mapService;

    public void setFeedService(FeedService feedService) {
        this.feedService = feedService;
    }

    public void setLocationService(LocationService locationService) {
        this.locationService = locationService;
    }

    @RequestMapping("/map")
    public String handleRequest(@RequestParam int routeId,
                                Map<String, Object> model,
                                HttpServletRequest request) {
        List<LiveLocationFeed> latestLocations = locationService.getLatestLocationOfAllAthletes(routeId);
        if (latestLocations != null) {
            model.put("locationFeeds", latestLocations);
        }
        List<Feed> feedList = feedService.getLatestFeedsOfOneRoute(routeId);
        if (feedList != null) {
            model.put("feedList", feedList);
        }

        try {
            model.put("mapPoints", mapService.getTruncatedGeoPointsInitializeIfNotExist(routeId, request.getSession().getServletContext()));
        } catch (Exception ex) {

        }

        model.put("routeMapPath", routeService.getMapPublicPath(routeId));
        String serverName = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
        model.put("serverName", serverName);
        model.put("routeId", routeId);
        return "map";
    }
}
